package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {


    @Autowired
    private VideoManager videoManager;



    //查询推荐视频
    @GetMapping("/smallVideos")
    public PageBeanVo findVideo(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        //1. 获取登录用户id
        Long userId = UserHolder.get().getId();

        return videoManager.findVideo(userId,pageNum, pageSize);
    }

    //上传视频
    @PostMapping("/smallVideos")
    public void saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videoManager.saveVideo(videoThumbnail,videoFile);
    }

    //添加关注
    @PostMapping("/smallVideos/{uid}/userFocus")
    public void saveUserFocus(@PathVariable("uid") Long focusUserId){
        videoManager.saveUserFocus(focusUserId);
    }

    //删除关注
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public void deleteUserFocus(@PathVariable("uid") Long focusUserId){
        videoManager.deleteUserFocus(focusUserId);
    }

    //视频点赞
    @PostMapping("/smallVideos/{id}/like")
    public void videoLike(@PathVariable("id") String videoId){
        videoManager.videoLike(videoId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{id}/dislike")
    public void deleteVideoLike(@PathVariable("id") String videoId){
        videoManager.deleteVideoLike(videoId);
    }

    //视频评论查询
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo findComments(
            @RequestParam("page") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize,
            @PathVariable("id") String videoId
    ){
        return videoManager.findComments(videoId,pageNum,pageSize);
    }

    //保存评论
    @PostMapping("/smallVideos/{id}/comments")
    public void saveVideoComment(
            @PathVariable("id") String videoId,
            @RequestBody Map<String,String> map
            ){
        String content = map.get("comment");
        //调用manager
        videoManager.saveVideoComment(videoId,content);
    }

}
